<?php
/** 
* 公共方法 
* 
* @author      Ray<jianglei@tiantu.in> 
* @version     $Id$ 
* @date        2017-4-14 13:27
* @since       1.0 
*/

/** 
* curlData
* curl post data 
* 
* @param string $url  
* @param array $data post的数据 
* @param array $header post是传输的header信息 
* @since 1.0 
* @return boolen
*/
function curlData($url, $data='', $headers = array()){

    if(!$url || !$data) return false;

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
    if ($headers) {
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    }
    $response = curl_exec($ch);


    curl_close($ch);
    return $response;

}

/**
* 检查手机号码
* @param unknown $mobile
* @return boolean 
*/
function checkMobile($mobile){
    return preg_match("/^(0|86|17951)?(13[0-9]|15[0-9]|18[0-9]|14[0-9]|17[0-9])[0-9]{8}$/",$mobile) ? true:false;
}

function xmlstr_to_array($xmlstr) {
  $doc = new DOMDocument();
  $doc->loadXML($xmlstr);
  $root = $doc->documentElement;
  $output = domnode_to_array($root);
  $output['root'] = $root->tagName;
  return $output;
}
function domnode_to_array($node) {
  $output = array();
  switch ($node->nodeType) {
    case XML_CDATA_SECTION_NODE:
    case XML_TEXT_NODE:
      $output = trim($node->textContent);
    break;
    case XML_ELEMENT_NODE:
      for ($i=0, $m=$node->childNodes->length; $i<$m; $i++) {
        $child = $node->childNodes->item($i);
        $v = domnode_to_array($child);
        if(isset($child->tagName)) {
          $t = $child->tagName;
          if(!isset($output[$t])) {
            $output[$t] = array();
          }
          $output[$t][] = $v;
        }
        elseif($v || $v === '0') {
          $output = (string) $v;
        }
      }
      if($node->attributes->length && !is_array($output)) { //Has attributes but isn't an array
        $output = array('@content'=>$output); //Change output into an array.
      }
      if(is_array($output)) {
        if($node->attributes->length) {
          $a = array();
          foreach($node->attributes as $attrName => $attrNode) {
            $a[$attrName] = (string) $attrNode->value;
          }
          $output['@attributes'] = $a;
        }
        foreach ($output as $t => $v) {
          if(is_array($v) && count($v)==1 && $t!='@attributes') {
            $output[$t] = $v[0];
          }
        }
      }
    break;
  }
  return $output;
}
function createVerify()
{
    $rand = mt_rand(100000, 999999);
    return $rand;
}
function curl_get($durl,$timeOut=10){
    if(!$durl) return false;

    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, $durl);
    curl_setopt($ch, CURLOPT_TIMEOUT, $timeOut);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.116 Safari/537.36");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
    $r = curl_exec($ch);

    return $r;
}
function getLnglat($province, $city, $area='', $place = ''){
          if(!$province || !$city) return ['lng'=>0,'lat'=>0];

          if($city == '市辖区' || $city == '县') $city = $province;

          $baidu_ak = 'jZGYc9DeH9ZE21uAReQrqWGxvDO2UGA7';
          $bmapurl = 'http://api.map.baidu.com/geocoder/v2/?output=json&ak=';
          if($area == '市辖区') $area = '';
          $url = $bmapurl.$baidu_ak.'&address='.$city.$area.$place;
          $data = json_decode(curl_get($url),true);
          if($data['status']){
             $url = $bmapurl.$baidu_ak.'&address='.$province.$city;
             $data = json_decode(curl_get($url),true);
          }
          if($data['status']){
           $lng = 0;
           $lat = 0;
          }else{
           $lng = $data['result']['location']['lng'];
           $lat = $data['result']['location']['lat'];
          }
          return ['lng'=>$lng,'lat'=>$lat];
     }
function getLnglatDistance($blat = 0,$blng = 0,$elat = 0,$elng = 0){
    if(!$blat || !$blng || !$elat || !$elng) return 0;

         $baidu_ak = 'jZGYc9DeH9ZE21uAReQrqWGxvDO2UGA7';
         $bmapurl = 'http://api.map.baidu.com/routematrix/v2/driving?output=json&origins=';
         $origins = $blat.','.$blng;
         $destinations= $elat.','.$elng;
         $url = $bmapurl.$origins.'&destinations='.$destinations.'&ak='.$baidu_ak;
         $data = json_decode(curl_get($url),true);
         if($data['status']){
            return 0;
         }else{
            return round($data['result'][0]['distance']['value']*0.001,1);
         }
}
